﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Brain.Rendering;

namespace Brain
{
    public class BezierCurve2D : ISprite, ICustomDrawSprite
    {
        public int Tesselation = 36;

        public List<BezierPoint2D> Points;

        public Vector2 Point1;
        public Vector2 Control1;
        public Vector2 Control2;
        public Vector2 Point2;

        public BezierCurve2D()
        {
            Points = new List<BezierPoint2D>();
        }

        public void AddPoint(Vector2 point, Vector2 control)
        {
            Points.Add(new BezierPoint2D(point, control));
        }

        public void Draw()
        {
        }

        public void Draw(IDrawer2D drawer)
        {
            if (Points.Count < 2)
            {
                return;
            }

            BezierPoint2D point0 = Points[0];
            BezierPoint2D point1 = Points[1];
            Vector2 q0 = Vector2Util.BezierPoint(0, point0.Point, point0.Control,
                point1.Control, point1.Point);
            for (int i = 0; i < Tesselation; i++)
            {
                float t = i / (float)Tesselation;
                Vector2 q1 = Vector2Util.BezierPoint(t, point0.Point, point0.Control,
                    point1.Control, point1.Point);
                drawer.DrawLine(q0, q1, Color.Red, 2);

                q0 = q1;
            }

            for (int i = 2; i < Points.Count; i++)
            {
                BezierPoint2D point = Points[i];

                q0 = Vector2Util.BezierPoint(0, point.Point, point.Control,
                        point1.Control, point1.Point);

                for (int x = 0; x < Tesselation; x++)
                {
                    float t = x / (float)Tesselation;
                    Vector2 q1 = Vector2Util.BezierPoint(t, point.Point, point.Control,
                        point1.Control, point1.Point);
                    drawer.DrawLine(q0, q1, Color.Red, 2);

                    q0 = q1;
                }
                point1 = point;
            }

            /*Vector2 q0 = Vector2Util.BezierPoint(0, Point1, Control1, Control2, Point2);

            for (int i = 0; i < Tesselation; i++)
            {
                float t = i / (float)Tesselation;
                Vector2 q1 = Vector2Util.BezierPoint(t, Point1, Control1, Control2, Point2);
                drawer.DrawLine(q0, q1, Color.Red, 1);

                q0 = q1;
            }

#if DEBUG
            int size = 16;
            int size2 = size / 2;
            drawer.DrawRectangle(new Rectangle((int)Point1.X - size2,
                (int)Point1.Y - size2, size, size), Color.Blue);
            drawer.DrawRectangle(new Rectangle((int)Point2.X - size2,
                (int)Point2.Y - size2, size, size), Color.Blue);
#endif
             */
        }

        public Rectangle BoundsRectangle { get; set; }
        public Vector2 StereoOffset { get; set; }

        private SpriteRendering rendering;
        public SpriteRendering Rendering
        {
            get { return rendering; }
            set
            {
                if (Engine.Instance.RenderManager2D != null)
                {
                    if (value == SpriteRendering.Invisible &&
                        value != rendering)
                    {
                        if (Engine.Instance.Editor == null)
                        {
                            Engine.Instance.RenderManager2D.RemoveSprite(this);
                        }
                    }
                    if (value == SpriteRendering.Rendered && value != rendering ||
                        value == SpriteRendering.EditorVisible && Engine.Instance.Editor != null)
                    {
                        Engine.Instance.RenderManager2D.SubmitSprite(this);
                    }
                    rendering = value;
                }
            }
        }

        public Color Color { get; set; }

        public float RotationAngle { get; set; }

        public SpriteEffects SpriteEffects { get; set; }

        public Vector2? Origin { get; set; }

        public bool ShouldDraw { get; set; }
        public bool ShouldUpdate { get; set; }
        public void Update()
        {
        }
    }
}
